package api

import (
	"strconv"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva_intelligent_form/model/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type IntelligentFormApi struct{}

// CreateIntelligentForm 创建表单
// @Tags IntelligentForm
// @Summary 创建表单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CreateIntelligentFormRequest true "创建表单"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /intelligent-form/form/create [post]
func (api *IntelligentFormApi) CreateIntelligentForm(c *gin.Context) {
	var req request.CreateIntelligentFormRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	userId := utils.GetUserID(c)

	form, err := intelligentFormService.CreateIntelligentForm(&req, userId)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}

	response.OkWithData(form, c)
}

// UpdateIntelligentForm 更新表单
// @Tags IntelligentForm
// @Summary 更新表单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.UpdateIntelligentFormRequest true "更新表单"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /intelligent-form/form/update [put]
func (api *IntelligentFormApi) UpdateIntelligentForm(c *gin.Context) {
	var req request.UpdateIntelligentFormRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	userId := utils.GetUserID(c)

	err = intelligentFormService.UpdateIntelligentForm(&req, userId)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("更新成功", c)
}

// DeleteIntelligentForm 删除表单
// @Tags IntelligentForm
// @Summary 删除表单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id query uint true "表单ID"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /intelligent-form/form/delete [delete]
func (api *IntelligentFormApi) DeleteIntelligentForm(c *gin.Context) {
	idStr := c.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	// 获取当前用户ID
	userId := utils.GetUserID(c)

	err = intelligentFormService.DeleteIntelligentForm(uint(id), userId)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("删除成功", c)
}

// GetIntelligentForm 获取表单详情
// @Tags IntelligentForm
// @Summary 获取表单详情
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id query uint true "表单ID"
// @Success 200 {object} response.Response{data=model.IntelligentForm,msg=string} "查询成功"
// @Router /intelligent-form/form/detail [get]
func (api *IntelligentFormApi) GetIntelligentForm(c *gin.Context) {
	idStr := c.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	form, err := intelligentFormService.GetIntelligentForm(uint(id))
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
		return
	}

	response.OkWithData(form, c)
}

// GetIntelligentFormList 获取表单列表
// @Tags IntelligentForm
// @Summary 获取表单列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.IntelligentFormSearch true "获取表单列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /intelligent-form/form/list [get]
func (api *IntelligentFormApi) GetIntelligentFormList(c *gin.Context) {
	var pageInfo request.IntelligentFormSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 默认只显示当前用户创建的表单
	if pageInfo.CreateUser == 0 {
		pageInfo.CreateUser = utils.GetUserID(c)
	}

	list, total, err := intelligentFormService.GetIntelligentFormList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// PublishIntelligentForm 发布表单
// @Tags IntelligentForm
// @Summary 发布表单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.PublishFormRequest true "发布表单"
// @Success 200 {object} response.Response{msg=string} "发布成功"
// @Router /intelligent-form/form/publish [post]
func (api *IntelligentFormApi) PublishIntelligentForm(c *gin.Context) {
	var req request.PublishFormRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	userId := utils.GetUserID(c)

	err = intelligentFormService.PublishIntelligentForm(req.ID, userId)
	if err != nil {
		global.GVA_LOG.Error("发布失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("发布成功", c)
}

// CloseIntelligentForm 关闭表单
// @Tags IntelligentForm
// @Summary 关闭表单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CloseFormRequest true "关闭表单"
// @Success 200 {object} response.Response{msg=string} "关闭成功"
// @Router /intelligent-form/form/close [post]
func (api *IntelligentFormApi) CloseIntelligentForm(c *gin.Context) {
	var req request.CloseFormRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	userId := utils.GetUserID(c)

	err = intelligentFormService.CloseIntelligentForm(req.ID, userId)
	if err != nil {
		global.GVA_LOG.Error("关闭失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("关闭成功", c)
}

// GetPublicForm 获取公开表单（用于填写）
// @Tags IntelligentForm
// @Summary 获取公开表单
// @accept application/json
// @Produce application/json
// @Param id path uint true "表单ID"
// @Param password query string false "访问密码"
// @Success 200 {object} response.Response{data=model.IntelligentForm,msg=string} "获取成功"
// @Router /intelligent-form/public/form/{id} [get]
func (api *IntelligentFormApi) GetPublicForm(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	password := c.Query("password")

	form, err := intelligentFormService.GetPublicForm(uint(id), password)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(form, c)
}

// GetGeneratedForm 获取生成的表单（用于填写，无需认证）
// @Tags IntelligentForm
// @Summary 获取生成的表单
// @accept application/json
// @Produce application/json
// @Param id path uint true "表单ID"
// @Param password query string false "访问密码"
// @Success 200 {object} response.Response{data=model.IntelligentForm,msg=string} "获取成功"
// @Router /intelligent-form/generated/form/{id} [get]
func (api *IntelligentFormApi) GetGeneratedForm(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		response.FailWithMessage("参数错误", c)
		return
	}

	password := c.Query("password")

	form, err := intelligentFormService.GetGeneratedForm(uint(id), password)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(form, c)
}
